package com.itheima.bos.service.take_delivery.impl;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.take_delivery.WayBillRepository;
import com.itheima.bos.domain.take_delivery.WayBill;
import com.itheima.bos.index.WayBillIndexRepository;
import com.itheima.bos.service.take_delivery.WayBillService;

@Transactional
@Service
public class WayBillServiceImpl implements WayBillService {
	
	@Autowired
	private WayBillRepository wayBillRepository;
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;
	
	@Override
	public void save(WayBill model) {
		
		WayBill wayBill = wayBillRepository.findByWayBillNum(model.getWayBillNum());
		
		if (wayBill==null||wayBill.getId() == null) {
			wayBillRepository.save(model);
			wayBillIndexRepository.save(model);
		} else {	
			try {
				Integer id = wayBill.getId();
				BeanUtils.copyProperties(wayBill, model);
				wayBill.setId(id);
				wayBillIndexRepository.save(wayBill);
			} catch (Exception e) {	
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			} 
		}
	}
	@Override
	public WayBill findByWayBillNum(String wayBillNum) {
		
		return wayBillRepository.findByWayBillNum(wayBillNum);
	}
	@Override
	public Page<WayBill> findPageData(WayBill wayBill, Pageable pageable) {
		if (StringUtils.isBlank(wayBill.getWayBillNum())
				&&StringUtils.isBlank(wayBill.getSendAddress())
				&&StringUtils.isBlank(wayBill.getRecAddress())
				&&StringUtils.isBlank(wayBill.getSendProNum())
				&&(wayBill.getSignStatus()==null||wayBill.getSignStatus()==0)) {
			//无条件查询
			return wayBillRepository.findAll(pageable);
		} else {
			//布尔查询，多条件组合查询
			BoolQueryBuilder query = new BoolQueryBuilder();
			//完整词条查询
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				TermQueryBuilder termQuery = new TermQueryBuilder("wayBillNum",
						wayBill.getWayBillNum());
				query.must(termQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				//情况一;输入“北” 是查询词条一部分，使用模糊匹配词条查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder(
						"sendAddress","*" + wayBill.getSendAddress() + "*");
				//词条为长的例如：北京市海淀区
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(
						wayBill.getSendAddress()).field("sendAddress").defaultOperator(Operator.AND);
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryStringQueryBuilder);
				query.must(boolQueryBuilder);
			}
			if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
				WildcardQueryBuilder wildcardQuery = new WildcardQueryBuilder(
						"recAddress","*" + wayBill.getRecAddress() + "*");
				query.must(wildcardQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				TermQueryBuilder termQuery = new TermQueryBuilder("sendProNum",
						wayBill.getSendProNum());
				query.must(termQuery);
			}
			if (wayBill.getSignStatus() !=null && wayBill.getSignStatus() != 0) {
				TermQueryBuilder termQuery = new TermQueryBuilder("signStatus",
						wayBill.getSignStatus());
				query.must(termQuery);
			}
			SearchQuery searchQuery = new NativeSearchQuery(query);
			searchQuery.setPageable(pageable);//分页效果
			return wayBillIndexRepository.search(searchQuery);
		}
	}
	

}
